{{define "demo/gen/index"}}
    {{template "layout_form" .}}
{{end}}

{{define "js_common"}}
    {{block "select2" .}}{{end}}
{{end}}

{{define "content"}}
<div class="main-content">
    <div class="col-sm-12 alert alert-warning alert-dismissable">
        <button aria-hidden="true" data-dismiss="alert" class="close" type="button">x</button>
        <p>该功能主要是基于已存在的数据表，创建控制器、model、模板。没有那么智能，只是简单生成，</p>
        <p>创建后视图html可能需要自己进行修改，菜单需要自己在数据库中添加，路由也是需要自己在router/admin.go中添加</p>
        <p>注意：表的字段要添加注释信息</p>
    </div>
    <form class="form-horizontal m" id="form-add">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">选择数据库：</label>
            <div class="col-sm-6">
                <input type="hidden" name="db" id="db" value="">
                <select class="form-control" id="database" required style="max-width: 300px;display: inline-block">
                    <option value="">请选择选择数据库</option>
                    {{range $tag,$db := .dbList}}
                    <option value="{{$tag}}">[ {{$tag}} ] {{$db.DbName}}</option>
                    {{end}}
                </select>

                <a href="javascript:;" class="btn btn-sm btn-primary" onclick="getTableList()">获取表</a>
            </div>
        </div>
        <div id="tables_db_box" style="display: none;">
            <div class="form-group" id="tableList">
                <label class="col-sm-3 control-label is-required">选择表：</label>
                <div class="col-sm-9">
                    <select class="form-control select2" name="table" id="table" data-width="400px" data-clear="1" required>
                        <option value="">请选择要生成表</option>
                    </select>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 已经去除系统自带的表</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">功能标题：</label>
                <div class="col-sm-6">
                    <input type="text" name="title" class="form-control" placeholder="功能标题" id="title" required>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 该功能的名称在index.html中展示；例如：菜单、商品等</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">生成类名：</label>
                <div class="col-sm-6">
                    <input type="text" name="class" class="form-control" placeholder="生成类名" id="class" required>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 一般为去除表的前缀，例如：goods_info,生成控制器和模型名为GoodsInfo</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">所属模块：</label>
                <div class="col-sm-6">
                    <input type="text" name="modules" class="form-control" placeholder="所属模块" id="modules" required>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 相当于分组，控制器、模型、视图放在模块文件夹内。像系统模块为system</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">创建模式：</label>
                <div class="col-sm-6">
                    <select class="form-control" name="type" id="create_type">
                        <option value="0">全部</option>
                        <option value="1">控制器</option>
                        <option value="2">视图</option>
                        <option value="3">控制器+视图</option>
                        <option value="4">模型</option>
                    </select>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 默认创建为控制器+视图+模型</span>
                </div>
            </div>
            <div class="row m-t-md">
                <div class="col-sm-offset-5 col-sm-10">
                    <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>
                    <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
                </div>
            </div>
        </div>
    </form>
</div>
{{end}}
{{define "script"}}
    <script type="text/javascript">
        $(function (){
            $("#database").change(function (){
                dbChange();
            });
        })

        //数据库改变时，初始化清空
        function dbChange(){
            $("#tables_db_box").hide();
            $("#class").val('');
            $("#table").html('<option value="">请选择要生成表</option>').select2("destroy");
            $("#create_type").val('0')
        }

        //表改变时
        function select2change(obj){
            var id = obj.attr('id');
            var val = obj.val();
            if(id == 'table'){
                $("#class").val(val)
            }
        }

        //获取数据库表列表
        function getTableList(){
            var db = $("#database").val();
            $("#db").val(db)
            if (db == "" || !db){
                return false
            }
            $.operate.b5post("{{._controller_}}/table_list?db="+db,{},function (result){
                if (result.success && result.data.length > 0){
                    $("#tables_db_box").show();
                    var html = '<option value="">请选择要生成表</option>';
                    for (var i = 0; i < result.data.length; i++) {
                        html += '<option value="'+result.data[i]+'">'+result.data[i]+'</option>'
                    }
                    $("#table").html(html);
                    select2Default("table",true,"")
                }
            })
        }
        function submitHandler() {
            if ($.validate.form()) {
                $.operate.saveTab("{{._controller_}}/create", $('#form-add').serialize());
            }
        }
    </script>
{{end}}
